package com.one.group.admin.controller;


import com.one.group.result.Result;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.TencentCOS;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * {@code @Create:}  2025-09-28  14 : 55
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    private Logger log= LoggerFactory.getLogger(UploadController.class);

    //配置上传文件的后缀
    private static final List<String> EXT_LIT= Arrays.asList(".jpg",".png",".gif");


    @RequestMapping(value = "/image",method = RequestMethod.POST)
    public Result upload(@RequestParam MultipartFile image) throws IOException {

        //1.获取原来的名字
        String originalFilename = image.getOriginalFilename();
        log.debug("originalFilename:{}",originalFilename);

        //2.获取后缀
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.debug("上传文件的后缀:{}",extName);

        //3.判断类型是否合法
        if(!EXT_LIT.contains(extName)){
            log.info("上传图片格式不合法!");
            return ResultUtil.success(20001,"上传图片格式不合法!");
        }

        //4.生成新的名字
        String picName = UUID.randomUUID().toString().replace("-", "")+extName;
        log.debug("生成的图片名称为:{}",picName);

        //5.创建临时File对象
        File tempFile = File.createTempFile("image-", picName);

        //6.把图片存储到临时的File对象中
        image.transferTo(tempFile);

        //7.把临时的File对象通过腾讯云api上传到腾讯服务器
        String fileName = TencentCOS.uploadfile(tempFile, "hospital");

        //8.删除临时的file对象
        if(tempFile.exists()){
            tempFile.delete();
        }

        //9.返回图片的访问地址
        return ResultUtil.success("https://furong-1378061083.cos.ap-chengdu.myqcloud.com/"+fileName);
    }
    
    /**
     * 装修验收照片上传（支持多图）
     */
    @RequestMapping(value = "/decoration-inspection-photos",method = RequestMethod.POST)
    public Result uploadDecorationInspectionPhotos(@RequestParam MultipartFile[] photos) throws IOException {
        if (photos == null || photos.length == 0) {
            return ResultUtil.success(20001, "请选择要上传的照片");
        }
        
        StringBuilder urls = new StringBuilder();
        String prefix = "https://furong-1378061083.cos.ap-chengdu.myqcloud.com/";
        String pathPrefix = "decoration-inspection";
        
        for (int i = 0; i < photos.length; i++) {
            MultipartFile photo = photos[i];
            
            // 获取文件后缀
            String originalFilename = photo.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
            
            // 检查文件格式
            if(!EXT_LIT.contains(extName)){
                continue; // 跳过不合法的文件
            }
            
            // 生成新文件名
            String picName = UUID.randomUUID().toString().replace("-","")+extName;
            
            // 创建临时文件
            File tempFile = File.createTempFile("inspection-", picName);
            photo.transferTo(tempFile);
            
            // 上传到腾讯云
            String fileName = TencentCOS.uploadfile(tempFile, pathPrefix);
            
            // 删除临时文件
            if(tempFile.exists()){
                tempFile.delete();
            }
            
            // 添加URL到结果中
            urls.append(prefix).append(fileName);
            if (i < photos.length - 1) {
                urls.append(",");
            }
        }
        
        return ResultUtil.success(urls.toString());
    }
    
    /**
     * 装修图纸上传
     */
    @RequestMapping(value = "/decoration-drawings",method = RequestMethod.POST)
    public Result uploadDecorationDrawings(@RequestParam MultipartFile[] drawings) throws IOException {
        if (drawings == null || drawings.length == 0) {
            return ResultUtil.success(20001, "请选择要上传的图纸");
        }
        
        StringBuilder urls = new StringBuilder();
        String prefix = "https://furong-1378061083.cos.ap-chengdu.myqcloud.com/";
        String pathPrefix = "decoration-drawings";
        
        for (int i = 0; i < drawings.length; i++) {
            MultipartFile drawing = drawings[i];
            
            // 获取文件后缀
            String originalFilename = drawing.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
            
            // 检查文件格式
            if(!EXT_LIT.contains(extName)){
                continue; // 跳过不合法的文件
            }
            
            // 生成新文件名
            String picName = UUID.randomUUID().toString().replace("-","")+extName;
            
            // 创建临时文件
            File tempFile = File.createTempFile("drawing-", picName);
            drawing.transferTo(tempFile);
            
            // 上传到腾讯云
            String fileName = TencentCOS.uploadfile(tempFile, pathPrefix);
            
            // 删除临时文件
            if(tempFile.exists()){
                tempFile.delete();
            }
            
            // 添加URL到结果中
            urls.append(prefix).append(fileName);
            if (i < drawings.length - 1) {
                urls.append(",");
            }
        }
        
        return ResultUtil.success(urls.toString());
    }



}
